

//
//  NewsImageViewController.m
//  WXMovie
//
//  Created by HZApple on 16/4/1.
//  Copyright © 2016年 无限互联. All rights reserved.
//

#import "NewsImageViewController.h"
#import "NewsImageCell.h"
#import "PhotoViewController.h"


#define kItemWidth 66
#define kItemHeight 50
#define kItemCount 4



#define kItemSpace (kScreenWidth - kItemWidth * kItemCount)/ (kItemCount + 5)


@interface NewsImageViewController ()

@property (nonatomic, copy) NSMutableArray *dataArray;

@end

@implementation NewsImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"图片新闻";
    _bImgView = [[BigNewsImageView alloc] init];
    
    [self.view addSubview:_bImgView];
    
    [self _loadData];
    
    [self _cofigUI];
    
    // Do any additional setup after loading the view.
}

- (void)_cofigUI {
//    @property (nonatomic) CGFloat minimumLineSpacing;
//    @property (nonatomic) CGFloat minimumInteritemSpacing;
//    @property (nonatomic) CGSize itemSize;
//    @property (nonatomic) CGSize estimatedItemSize
    self.collectionLayout.itemSize = CGSizeMake(kItemWidth, kItemHeight);
    self.collectionLayout.minimumLineSpacing = kItemSpace;
    self.collectionLayout.minimumInteritemSpacing = kItemSpace;
    self.collectionLayout.sectionInset = UIEdgeInsetsMake(kItemSpace, kItemSpace, 0, 0);
    
}

- (void)_loadData {
    
    _dataArray = [NSMutableArray array];
    
    NSArray *array = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_list" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil];
    
    for (NSDictionary * dic in array) {
        ImageModel *model = [[ImageModel alloc] init];
        model.imageId = [dic[@"id"] integerValue];
        model.imageName = dic[@"image"];
        model.imageType = [dic[@"type"] integerValue];
        
        [_dataArray addObject:model];
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewsImageCell" forIndexPath:indexPath];
    cell.imageModel = _dataArray[indexPath.row];
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PhotoViewController *photoVC = [[PhotoViewController alloc] init];
    //NSLog(@"%@",indexPath);
    photoVC.selectIndexPath = indexPath;
    NSLog(@"%@",photoVC.selectIndexPath);
    
    NSMutableArray *array = [NSMutableArray array];
    for (ImageModel *model in _dataArray) {
        [array addObject:model.imageName];
    }
    photoVC.imageURLArray = array;
    
    [self.navigationController pushViewController:photoVC animated:YES];
    
    
    //[self.navigationController pushViewController:photoVC animated:YES];
   
    
    
   // NSLog(@"%li",indexPath.row);
//    ImageModel *model = _dataArray[indexPath.row];
//    NSString *str = model.imageName;
//    [_bImgView setImageName:str];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
